package com.itboye.textileexpres.main;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseFragment;
import com.itboye.textileexpres.base.SpPublicParams;
import com.itboye.textileexpres.login.LoginActivity;
import com.itboye.textileexpres.logincontroller.LoginController;
import com.itboye.textileexpres.myset.ActivityAbout;
import com.itboye.textileexpres.myset.ActivityCustomerService;
import com.itboye.textileexpres.myset.ActivityFavorite;
import com.itboye.textileexpres.myset.ActivityKefu;
import com.itboye.textileexpres.myset.ActivityOffLine;
import com.itboye.textileexpres.myset.ActivityPersonDate;
import com.itboye.textileexpres.myset.ActivitySet;
import com.itboye.textileexpres.myset.ActivityUserProtocal;
import com.itboye.textileexpres.util.IsMobileNumPhone;
import com.itboye.textileexpres.util.SPUtils;
import com.itboye.textileexpres.util.XImageLoader;
import com.itboye.textileexpres.widget.RoundImageView;

public class MySetFragment extends BaseFragment implements OnClickListener {
	TextView title;
	ImageView back, img_other;
	RelativeLayout head_lin, set_rel, favorite_rel, user_rel, about_rel,
			order_rel;
	TextView name, phone, login_tv;
	RoundImageView head_img;
	String user_name;
	String phone_num;
	String head;

	/***
	 * 广播是否登录或者退出
	 */

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.fragment_myset;
	}

	@Override
	public void onMyActivityCreated() {
		// TODO Auto-generated method stub
		initent();

	}

	public void initent() {
		title.setText(R.string.set);
		back.setVisibility(View.GONE);
		user_name = (String) SPUtils.get(getActivity(), null,
				SpPublicParams.NICK_NAME, "");

		phone_num = (String) SPUtils.get(getActivity(), null,
				SpPublicParams.PHONE_MODEL, "");
		IntentFilter newshead = new IntentFilter("newshead");
		getActivity().registerReceiver(avartBroadcastReceiver, newshead);

		IntentFilter LoginSuccess = new IntentFilter("LoginSuccess");
		getActivity().registerReceiver(avartBroadcLoginSuccess, LoginSuccess);

		XImageLoader.load((String) SPUtils.get(getActivity(), null,
				SpPublicParams.URL, ""), head_img);

		if ("".equals(IsMobileNumPhone.isUid())
				|| IsMobileNumPhone.isUid() == "") {

			login_tv.setVisibility(View.VISIBLE);
			name.setVisibility(View.GONE);
			phone.setVisibility(View.GONE);
		} else {
			login_tv.setVisibility(View.GONE);
			name.setVisibility(View.VISIBLE);
			phone.setVisibility(View.VISIBLE);
			name.setText(user_name);
			phone.setText(phone_num);
		}

	}

	BroadcastReceiver avartBroadcastReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			XImageLoader.load((String) SPUtils.get(getActivity(), null,
					SpPublicParams.URL, ""), head_img);
			name.setVisibility(View.GONE);
			phone.setVisibility(View.GONE);
			login_tv.setVisibility(View.VISIBLE);
			IsMobileNumPhone.isUid();

		};
	};
	BroadcastReceiver avartBroadcLoginSuccess = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			XImageLoader.load((String) SPUtils.get(getActivity(), null,
					SpPublicParams.URL, ""), head_img);
			name.setVisibility(View.VISIBLE);
			phone.setVisibility(View.VISIBLE);
			login_tv.setVisibility(View.GONE);

		};
	};

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if ("".equals(IsMobileNumPhone.isUid())
				|| IsMobileNumPhone.isUid() == "") {

			login_tv.setVisibility(View.VISIBLE);
			name.setVisibility(View.GONE);
			phone.setVisibility(View.GONE);
		} else {
			login_tv.setVisibility(View.GONE);
			name.setVisibility(View.VISIBLE);
			phone.setVisibility(View.VISIBLE);
			name.setText(user_name);
			phone.setText(phone_num);
		}

		if (SPUtils.get(getActivity(), null, SpPublicParams.URL, "") == null) {

		} else if (SPUtils.get(getActivity(), null, SpPublicParams.USER_NAME,
				"") != null) {
			user_name = (String) SPUtils.get(getActivity(), null,
					SpPublicParams.NICK_NAME, "");
			phone_num = (String) SPUtils.get(getActivity(), null,
					SpPublicParams.PHONE_MODEL, "");
			phone.setText(phone_num);
			name.setText(user_name);
		}

		else {
			XImageLoader.load((String) SPUtils.get(getActivity(), null,
					SpPublicParams.URL, ""), head_img);
		}

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent;
		switch (v.getId()) {
		case R.id.head_lin:
			// LoginController.onPersonalData(getActivity(), null);
			if ("".equals(IsMobileNumPhone.isUid())
					|| IsMobileNumPhone.isUid() == "") {
				intent = new Intent(getActivity(), LoginActivity.class);
				startActivity(intent);

			} else {
				intent = new Intent(getActivity(), ActivityPersonDate.class);
				startActivity(intent);
			}
			break;
		case R.id.set_rel:
			intent = new Intent(getActivity(), ActivitySet.class);
			startActivity(intent);
			break;
		case R.id.favorite_rel:
			intent = new Intent(getActivity(), ActivityFavorite.class);
			startActivity(intent);
			// LoginController.onFavoriteData(getActivity(), null);
			break;
		case R.id.user_rel:
			intent = new Intent(getActivity(), ActivityUserProtocal.class);
			startActivity(intent);
			break;
		case R.id.about_rel:

			startActivity(new Intent(getActivity(), ActivityAbout.class));
			break;
		case R.id.img_other:
			intent = new Intent(getActivity(), ActivityCustomerService.class);
			startActivity(intent);

			// intent = new Intent(getActivity(), ActivityKefu.class);
			// startActivity(intent);
			break;
		case R.id.order_rel:
			intent = new Intent(getActivity(), ActivityOffLine.class);
			startActivity(intent);

			break;
		case R.id.head_img:
			// LoginController.onPersonalData(getActivity(), null);
			if ("".equals(IsMobileNumPhone.isUid())
					|| IsMobileNumPhone.isUid() == "") {
				intent = new Intent(getActivity(), LoginActivity.class);
				startActivity(intent);
			} else {
				intent = new Intent(getActivity(), ActivityPersonDate.class);
				startActivity(intent);
			}
			break;
		case R.id.login_tv:
			intent = new Intent(getActivity(), LoginActivity.class);
			startActivity(intent);
			break;
		default:
			break;
		}
	}
}
